package com.htfyun.unisound.serialmotor;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.WindowManager;

import com.htfyun.serialport.ServoSingleton;
import com.htfyun.unisound.serialmotor.fragment.TestFragment;
import com.viewpagerindicator.TabPageIndicator;


/**
 * Created by sst on 2018/7/11.
 */

public class MainActivity extends FragmentActivity {

    private final static String TAG = "EngineerTestMainActivity";
    private Context mContext;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_test);

        mContext = this;

        FragmentPagerAdapter adapter = new EngineerTestPagerAdapter(getSupportFragmentManager());

        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(pager);

    }

    @Override
    protected void onResume() {
        super.onResume();
        ServoSingleton.INSTANCE.init();

        initPermission();

    }

    @Override
    protected void onPause() {
        super.onPause();
        ServoSingleton.INSTANCE.deInit();

    }

    private void initPermission() {



        if (!isGotPermission()) {

            //同时申请多个权限
            PermissionManager.getInstance(getApplicationContext()).execute(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE);

        }

    }

    private boolean isGotPermission() {

        return (ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);

    }

    //如果有权限的话返回
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,  int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.getInstance(getApplicationContext()).
                onRequestPermissionsResult(requestCode, permissions, grantResults);

        DebugLog.e("onRequestPermissionsResult grantResults = " + grantResults);
    }

    private final class EngineerTestPagerAdapter extends FragmentPagerAdapter {
        public EngineerTestPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Class<? extends Fragment> cls = FragmentItemsEnum.values()[position].getFragmentClass();
            try {
                return cls.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return TestFragment.newInstance("Error cls = " + cls);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            int resId = FragmentItemsEnum.values()[position].getNameResId();
            return getString(resId);
        }

        @Override
        public int getCount() {
            return FragmentItemsEnum.values().length;
        }
    }

}
